package miniqq.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MyDBHelper extends SQLiteOpenHelper{
	private static String DATABASE_NAME;
	private static final int DATABASE_VERSION = 1;
	
	private static String DATABASE_TABLE_FRIEND = "CREATE TABLE Friend(" +
			"f_name varchar(20) NOT NULL PRIMARY KEY," +
			"phoneNum varchar(11) NOT NULL UNIQUE," +
			"image varchar(100));";
	
	private static String DATABASE_TABLE_COMMUNITY = "CREATE TABLE Community(" +
			"c_name varchar(20) NOT NULL PRIMARY KEY," +
			"leader varchar(11) NOT NULL);";
	
	private static String DATABASE_TABLE_MEMBERS = "CREATE TABLE Member(" +
			"c_name varchar(20) NOT NULL," +
			"f_name varchar(20) NOT NULL," +
			"PRIMARY KEY (c_name,f_name),"
			+ "FOREIGN KEY(c_name) REFERENCES Community(c_name),"
			+ "FOREIGN KEY(f_name) REFERENCES Friend(f_name));";
	
	private static String DATABASE_TABLE_PERSONAL_MESSAGES = "CREATE TABLE P_Message(" +
			"flag boolean NOT NULL," +
			"f_name varchar(20) NOT NULL," +
			"time DATETIME NOT NULL," +
			"content varchar(400) NOT NULL," +
			"PRIMARY KEY(time,content),"+
			"FOREIGN KEY (f_name) REFERENCES Friend(f_name));";
	
	private static String DATABASE_TABLE_COMMUNITY_MESSAGES = "CREATE TABLE C_Message(" +
			"s_name varchar(20) NOT NULL," +
			"time DATETIME NOT NULL," +
			"content varchar(400) NOT NULL," +
			"tag varchar(400) NOT NULL," +
			"PRIMARY KEY(time,content));";
	
	public MyDBHelper(Context context, String name, CursorFactory factory,
			int version,String user_name) {
		super(context, name, factory, version);
		DATABASE_NAME = user_name+".db";
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DATABASE_TABLE_COMMUNITY);
		db.execSQL(DATABASE_TABLE_COMMUNITY_MESSAGES);
		db.execSQL(DATABASE_TABLE_FRIEND);
		db.execSQL(DATABASE_TABLE_MEMBERS);
		db.execSQL(DATABASE_TABLE_PERSONAL_MESSAGES);
		Log.e("socket","database");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}

}
